package in.smartride.client;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.maps.client.InfoWindow;
import com.google.gwt.maps.client.InfoWindowContent;
import com.google.gwt.maps.client.MapWidget;
import com.google.gwt.maps.client.event.MarkerDragEndHandler;
import com.google.gwt.maps.client.event.MarkerDragStartHandler;
import com.google.gwt.maps.client.geom.LatLng;
import com.google.gwt.maps.client.overlay.Marker;
import com.google.gwt.maps.client.overlay.MarkerOptions;
import com.google.gwt.user.client.Command;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.DecoratorPanel;
import com.google.gwt.user.client.ui.DockPanel;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.RadioButton;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.FlexTable.FlexCellFormatter;
import com.google.gwt.user.datepicker.client.DateBox;

public class ShowMapBuilder implements Command {

	@Override
	public void execute() {
		RootPanel.get().clear();
		SmartRide.addBar();
		// TODO
		DockPanel panel = new DockPanel();
		// TODO add smthing
		MapWidget map = new MapWidget(com.google.gwt.maps.client.geom.LatLng
				.newInstance(17.3850440, 78.4866710), 13);
		map.setDraggable(true);
		map.setPixelSize(Window.getClientWidth()/2,Window.getClientHeight()/2 );
		panel.add(map, DockPanel.WEST);
		// Creating the other panel
		DecoratorPanel decPanel = new DecoratorPanel();

		FlexTable layout = new FlexTable();
		layout.setCellSpacing(6);
		FlexCellFormatter cellFormatter = layout.getFlexCellFormatter();

		// Add a title to the form
		cellFormatter.setColSpan(0, 0, 2);
		cellFormatter.setHorizontalAlignment(0, 0,
				HasHorizontalAlignment.ALIGN_CENTER);

		// Add some standard form options
		TextBox locationName = new TextBox();
		layout.setWidget(1, 1, locationName);
		
		// Wrap the content in a DecoratorPanel

		decPanel.setWidget(layout);

		panel.add(decPanel, DockPanel.EAST);

		RootPanel.get().add(panel,Window.getClientWidth()/4,Window.getClientHeight()/3);

		map.clearOverlays();

		MarkerOptions options = MarkerOptions.newInstance();
		options.setDraggable(true);
		final Marker marker = new Marker(map.getCenter(), options);
		final InfoWindow info = map.getInfoWindow();

		marker.addMarkerDragEndHandler(new MarkerDragEndHandler() {
			public void onDragEnd(MarkerDragEndEvent event) {
				info.open(marker, new InfoWindowContent(
						"Add this as the new landmark?"));
			}
		});

		marker.addMarkerDragStartHandler(new MarkerDragStartHandler() {
			public void onDragStart(MarkerDragStartEvent event) {
				info.setVisible(false);
			}
		});
		map.addOverlay(marker);
		LatLng latLang = marker.getLatLng();
		final double latitude = latLang.getLatitude();
		final double longitude = latLang.getLongitude();
		final String location = locationName.getValue();
		Button save = new Button();
		save.addClickHandler(new ClickHandler() {

			@Override
			public void onClick(ClickEvent event) {
				DataRetriever.saveNewLandMark(latitude, longitude, location);
			}
		});
		save.setText("save");
		save.setTitle("save");
		layout.setWidget(2, 1, save);
	}
}
